package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.OrderProduct;
import com.juzi.wopu.query.OrderProductQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.service.IOrderProductService;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class OrderProductAction extends CRUDAaction<OrderProduct> {
	
	private static final long serialVersionUID = 1L;

	private IOrderProductService orderProductService;

	private OrderProduct orderProduct = new OrderProduct();
	
	private OrderProductQuery baseQuery = new OrderProductQuery();
	
	private PageList pageList;
	
	private String totalNum;
	private String totalMoney;
	private String totalOrder;
	
	public void setOrderProductService(IOrderProductService orderProductService) {
		this.orderProductService = orderProductService;
	}
	
	@Override
	protected void list() {
		this.pageList = orderProductService.findByQuery(baseQuery);
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			totalOrder=pageList.getTotalcount()+"";
			Map<String, String> map=orderProductService.getTotalCount(baseQuery);
			totalMoney=map.get("totalMoney");
			totalNum=map.get("totalNum");
		}else {
			totalMoney="0.0";
			totalOrder="0";
			totalNum="0";
		}
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	public void validateSave() {

	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				orderProductService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {
			orderProduct = orderProductService.get(id);
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			orderProduct = new OrderProduct();
		} else {
			orderProduct = orderProductService.get(id);
		}
	}

	@Override
	public OrderProduct getModel() {
		return orderProduct;
	}

	public PageList getPageList() {
		return pageList;
	}

	public OrderProductQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(OrderProductQuery baseQuery) {
		this.baseQuery = baseQuery;
	}

	public String getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(String totalNum) {
		this.totalNum = totalNum;
	}

	public String getTotalMoney() {
		return totalMoney;
	}

	public void setTotalMoney(String totalMoney) {
		this.totalMoney = totalMoney;
	}

	public String getTotalOrder() {
		return totalOrder;
	}

	public void setTotalOrder(String totalOrder) {
		this.totalOrder = totalOrder;
	}

}
